﻿using System;
using System.Collections.Generic;
using MagicWorld.DDD.Domain.Types;

namespace MagicWorld.DDD.Domain.Service
{
    public class MovementSystem
    {
        private const long X_FENCE_MIN = -100;
        private const long X_FENCE_MAX = 100;
        private const long Y_FENCE_MIN = -100;
        private const long Y_FENCE_MAX = 100;
        private readonly List<IMovable> entities = new List<IMovable>();

        public void Register(IMovable movable)
        {
            entities.Add(movable);
        }

        public void Update()
        {
            foreach (var entity in entities)
            {
                if (!entity.IsMoving()) continue;
                
                Transform old = entity.GetPosition();
                Vector vel = entity.GetVelocity();
                long newX = Math.Max(Math.Min(old.X + vel.X, X_FENCE_MAX), X_FENCE_MIN);
                long newY = Math.Max(Math.Min(old.Y + vel.Y, Y_FENCE_MAX), Y_FENCE_MIN);
                entity.MoveTo(newX, newY);
            }
        }
    }
}